<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Admin\Tools\Audit\ExpenseAuditTool;
use App\Models\ContractedCustomerModel;
use App\Models\FinanceExpensesModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\Facades\Auth;

class AuditExpenseController extends AdminController
{
    protected $title = '财务支出审核';

    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header($this->title);
            $content->description('列表');
            $content->body($this->grid());
        });
    }

    public function grid()
    {
        $tab = new Tab();
        $tab->add('待审核', $this->notAudit());
        $tab->add('已通过', $this->auditSuccess());
        $tab->add('已拒绝', $this->auditFailed());
        return $tab->render();
    }

    public function auditFailed(): NGrid
    {
        $grid = new NGrid(new FinanceExpensesModel());
        $grid->model()
            ->leftJoin('contracted_customer', 'contracted_customer.id', 'finance_expenses.contracted_customer_id')
            ->leftJoin('admin_users', 'admin_users.id', 'contracted_customer.operate_id')
            ->where('finance_expenses.status', 2)
            ->orderByDesc('finance_expenses.id')
            ->select([
                'finance_expenses.*',
                'admin_users.name as operate_name',
                'contracted_customer.male_name as male_name',
                'contracted_customer.female_name as female_name',
                'contract_no',
                'contract_amount',
            ]);

//            if (!ISADMIN) {
//                if (ISMAIN) $grid->model()->where('finance_expenses.main_id', USERID);
//                else $grid->model()->where('finance_expenses.main_id', MAINID);
//            }else $grid->model()->where('finance_expenses.operate_id', USERID);

        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('finance_expenses.main_id', MAINID);
            else $grid->model()->where('finance_expenses.operate_id', USERID);
        }

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('contracted_customer_id', '客户信息')->select((new ContractedCustomerModel)->customerSelect());
                $filter->date('income_date', __('finance_income.incomeDate'));
            });
        });
        if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
//        if (in_array(Auth::user()->roles->first()->slug, ['Administrator', 'boss', 'finance'])) $grid->column('id', 'ID')->sortable();
        $grid->column('name', '名称');
        $grid->column('customer_info', '客户信息')->display(function () {
            return '合同编号：' . $this->contract_no . ';男方姓名：'
                . $this->male_name . ';女方姓名：' . $this->female_name . ';合同金额：' . $this->contract_amount;
        });
        $grid->column('num', '数量');
        $grid->column('amount', '金额');
        $grid->column('date', '支出日期');
        $grid->column('type', '类别')->using(__('finance_expenses.typeArr'));
        $grid->column('invoice_type', '发票类型')->using(__('finance_expenses.invoiceTypeArr'));
        $grid->column('invoice_proof', '凭证')->display(function () {
            if (empty($this->invoice_proof)) return null;
            else {
                $arr = [];
                foreach ($this->invoice_proof as $v) {
                    $arr[] = config('env.app_url') . '/upload/' . $v;
                }
                return view('images', ['id' => $this->id, 'image' => $arr]);
            }
        });
        $grid->column('remark', __('l.remark'));
        $grid->column('status', __('l.status'))->using(__('l.auditAllStatusArr'));
        $grid->column('audit_remark', __('finance_income.auditRemark'));
        $grid->column('created_at', __('l.createdAt'))->display(function () {
            return date('Y-m-d H:i:s', strtotime($this->created_at));
        });
        $grid->disableActions();
        $grid->disableCreateButton();
        return $grid;
    }

    public function auditSuccess(): NGrid
    {
        $grid = new NGrid(new FinanceExpensesModel());
        $grid->model()
            ->leftJoin('contracted_customer', 'contracted_customer.id', 'finance_expenses.contracted_customer_id')
            ->leftJoin('admin_users', 'admin_users.id', 'contracted_customer.operate_id')
            ->where('finance_expenses.status', 1)
            ->orderByDesc('finance_expenses.id')
            ->select([
                'finance_expenses.*',
                'admin_users.name as operate_name',
                'contracted_customer.male_name as male_name',
                'contracted_customer.female_name as female_name',
                'contract_no',
                'contract_amount',
            ]);
        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('finance_expenses.main_id', MAINID);
            else $grid->model()->where('finance_expenses.operate_id', USERID);
        }
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('contracted_customer_id', '客户信息')->select((new ContractedCustomerModel)->customerSelect());
                $filter->date('income_date', __('finance_income.incomeDate'));
            });
        });

        if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('name', '名称');
        $grid->column('customer_info', '客户信息')->display(function () {
            return '合同编号：' . $this->contract_no . ';男方姓名：'
                . $this->male_name . ';女方姓名：' . $this->female_name . ';合同金额：' . $this->contract_amount;
        });
        $grid->column('num', '数量');
        $grid->column('amount', '金额');
        $grid->column('date', '支出日期');
        $grid->column('type', '类别')->using(__('finance_expenses.typeArr'));
        $grid->column('invoice_type', '发票类型')->using(__('finance_expenses.invoiceTypeArr'));
        $grid->column('invoice_proof', '凭证')->display(function () {
            if (empty($this->invoice_proof)) return null;
            else {
                $arr = [];
                foreach ($this->invoice_proof as $v) {
                    $arr[] = config('env.app_url') . '/upload/' . $v;
                }
                return view('images', ['id' => $this->id, 'image' => $arr]);
            }
        });
        $grid->column('remark', __('l.remark'));
        $grid->column('status', __('l.status'))->using(__('l.auditAllStatusArr'));
        $grid->column('audit_remark', __('finance_income.auditRemark'));
        $grid->column('created_at', __('l.createdAt'))->display(function () {
            return date('Y-m-d H:i:s', strtotime($this->created_at));
        });
        $grid->disableActions();
        $grid->disableCreateButton();
        return $grid;
    }

    public function notAudit(): NGrid
    {
        $grid = new NGrid(new FinanceExpensesModel());
        $grid->model()
            ->leftJoin('contracted_customer', 'contracted_customer.id', 'finance_expenses.contracted_customer_id')
            ->leftJoin('admin_users', 'admin_users.id', 'contracted_customer.operate_id')
            ->where('finance_expenses.status', 0)
            ->orderByDesc('finance_expenses.id')
            ->select([
                'finance_expenses.*',
                'admin_users.name as operate_name',
                'contracted_customer.male_name as male_name',
                'contracted_customer.female_name as female_name',
                'contract_no',
                'contract_amount',
            ]);
        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('finance_expenses.main_id', MAINID);
            else $grid->model()->where('finance_expenses.operate_id', USERID);
        }
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('contracted_customer_id', '客户信息')->select((new ContractedCustomerModel)->customerSelect());
                $filter->date('income_date', __('finance_income.incomeDate'));
                $filter->equal('status', __('l.status'))->select(__('l.auditAllStatusArr'));
            });
        });
        if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
        $grid->column('name', '名称');
        $grid->column('customer_info', '客户信息')->display(function () {
            return '合同编号：' . $this->contract_no . ';男方姓名：'
                . $this->male_name . ';女方姓名：' . $this->female_name . ';合同金额：' . $this->contract_amount;
        });
        $grid->column('num', '数量');
        $grid->column('amount', '金额');
        $grid->column('date', '支出日期');
        $grid->column('type', '类别')->using(__('finance_expenses.typeArr'));
        $grid->column('invoice_type', '发票类型')->using(__('finance_expenses.invoiceTypeArr'));
        $grid->column('invoice_proof', '凭证')->display(function () {
            if (empty($this->invoice_proof)) return null;
            else {
                $arr = [];
                foreach ($this->invoice_proof as $v) {
                    $arr[] = config('env.app_url') . '/upload/' . $v;
                }
                return view('images', ['id' => $this->id, 'image' => $arr]);
            }
        });
        $grid->column('remark', __('l.remark'));
        $grid->column('status', __('l.status'))->using(__('l.auditAllStatusArr'));
        $grid->column('audit_remark', __('finance_income.auditRemark'));
        $grid->column('created_at', __('l.createdAt'))->display(function () {
            return date('Y-m-d H:i:s', strtotime($this->created_at));
        });
        $grid->actions(function (Actions $action) {
            $action->disableEdit();
            $action->disableDelete();
            $action->disableView();
            if ((int)$action->row->status === 0) $action->append(new ExpenseAuditTool($action->row->id));
        });
        $grid->disableActions(false);
        $grid->disableCreateButton();
        return $grid;
    }
}
